﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WAVEZ
{
    static class SceneManager
    {
        private static SceneBase current;

        private static bool gamePaused = false;
        private static bool isGameLevel;
        private static KeyboardState KeyboardAntigo, KeyboardAtual;
        private static GamePadState GamePadAntigo, GamePadAtual;
        

        public static void setScene(SceneBase scene, bool isGameLevel)
        {
            SceneManager.isGameLevel = isGameLevel;

            if (current != null)
                current.terminate();

            current = scene;

            if (current != null)
                current.start();
        }

        public static bool update(GameTime gameTime)
        {

            if (current == null)
            {
                return false;
            }
            else
            {
                KeyboardAtual = Keyboard.GetState();

                GamePadAtual = GamePad.GetState(PlayerIndex.One);


                if (gamePaused != true)
                {
                    if (((KeyboardAtual.IsKeyDown(Keys.Escape)) && KeyboardAntigo.IsKeyUp(Keys.Escape) && (isGameLevel == true)) || ((GamePadAtual.IsButtonDown(Buttons.Start)) && (GamePadAntigo.IsButtonUp(Buttons.Start)) && (isGameLevel == true)))
                    {
                        gamePaused = true;
                    }

                    current.update(gameTime);
                }

                else
                {

                    if (((KeyboardAtual.IsKeyDown(Keys.Escape)) && (KeyboardAntigo.IsKeyUp(Keys.Escape)) && (isGameLevel == true)) || ((GamePadAtual.IsButtonDown(Buttons.Start)) && (GamePadAntigo.IsButtonUp(Buttons.Start)) && (isGameLevel == true)))
                    {
                        gamePaused = false;
                    }
                }

                KeyboardAntigo = KeyboardAtual;

                GamePadAntigo = GamePadAtual;

                return true;
            }

        }

        public static bool draw(SpriteBatch spriteBatch)
        {
            if (current == null)
            {
                return false;
            }
            else
            {
                current.draw(spriteBatch);

                if (gamePaused == true)
                {
                    
                }
                return true;
            }
        }
    }
}
